import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:jetour_line/my_colors.dart';
import 'package:jetour_line/utils/aha_utils.dart';
import 'package:jetour_line/utils/extension_int.dart';
import 'package:jetour_line/utils/page_push_utils.dart';

import '../../../base_widget/banner_widget.dart';
import '../../../base_widget/base_image.dart';
import '../../../base_widget/sliver_list_widget.dart';
import '../../../model/line_detail_model.dart';
import 'line_detali_image_page.dart';

class LineDetailHeadWidget extends StatefulWidget {
  final LineDetailModel lineDetailModel;

  LineDetailHeadWidget({Key? key, required this.lineDetailModel})
      : super(key: key);

  @override
  State<LineDetailHeadWidget> createState() => _LineDetailHeadWidgetState();
}

class _LineDetailHeadWidgetState extends State<LineDetailHeadWidget> {
  List<String> images = [] ;
  @override
  Widget build(BuildContext context) {
    if(images.isEmpty){
      int? count = widget.lineDetailModel.swiper?.length;
      for(int i = 0;i<count!;i++){
        Swiper? swiper = widget.lineDetailModel.swiper![i] ;
        String? url = swiper?.url;
        images.add(url!);
      }
    }
    return SliverListWidget.getSliverList(1, (context, index) {
      return Padding(
        padding: EdgeInsets.only(bottom: 40.px),
        child: Stack(children: [
          BannerWidget(images,width: double.infinity,height: 480.px,selIndexCallBack: (int index){
            PagePushUtils.push(context, LineDetailImageScreen(images: images,index: index,));
          },),
          Positioned(
            right: 20.px,
            top: 348.px,
            child: Stack(alignment: Alignment.center, children: [
              ClipRRect(
                borderRadius: BorderRadius.all(Radius.circular(24.px)),
                child: Opacity(
                  opacity: 0.8,
                  child: Container(
                    width: 100.px,
                    height: 48.px,
                    color: MyColors.color1A1A1A,
                  ),
                ),
              ),
              Row(children: [
                // ignore: prefer_const_constructors
                BaseImage(image:'icon_jdphoto',width: 32.px, height: 32.px,),
                Text("${images.length}",style: TextStyle(fontSize: 24.px,color: MyColors.white),),
              ],),
            ],),
          ),
          Column(children: [
            SizedBox(height: 416.px,),
            titleWidget()
          ],)

          // titleWidget(),
        ],),
      );
    });
  }

  Widget titleWidget(){
    String? caption = widget.lineDetailModel.caption;
    List<String>? tag = widget.lineDetailModel.tag;
    int leng  = 0;
    int tagCount = 0;
    for(int i = 0;i<(tag! as List).length;i++){
      leng = tag[i].length+leng;
      if(leng>24){
        i = tag.length;
         continue;
      }
      tagCount ++;
    }

    String? msg = '       ${widget.lineDetailModel.description}';
    return  ClipRRect(
      borderRadius: BorderRadius.only(topLeft: Radius.circular(56.px),
          topRight: Radius.circular(56.px)),
      child: Container(
        padding: EdgeInsets.only(top: 40.px,left: 24.px,right: 24.px,bottom: 24.px),
        color: MyColors.white,
        child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
          Text.rich(
            TextSpan(children: [
              if(widget.lineDetailModel.totalFee!>0)
                WidgetSpan(
                    child: Padding(
                      padding: EdgeInsets.only(right: 10.px,bottom: 4.px),
                      child: ClipRRect(
                        borderRadius: BorderRadius.all(Radius.circular(4.px)),
                        child: Container(
                          alignment: Alignment.center,
                          width: 60.px,
                          height: 30.px,
                          color: MyColors.colorEE7219,
                          child: Text('VIP',style: TextStyle(fontSize: 22.px,color: MyColors.white),),
                        ),
                      ),
                    )),
              TextSpan(
                  text: caption,
                  style: TextStyle(
                      fontSize: 30.px,
                      color: MyColors.color1A1A1A,fontWeight: FontWeight.w500)),
            ]),
            maxLines: 4,
            overflow: TextOverflow.ellipsis,
          ),
          if(tag.isNotEmpty)
            Padding(
              padding: EdgeInsets.only(top: 26.px),
              child: Row( children: [
                for(int i = 0;i<tagCount;i++)
                  Container(
                      padding: EdgeInsets.all(8.rpx),
                      margin: EdgeInsets.only(right: 16.rpx),
                      decoration: BoxDecoration(
                          color: const Color(0xFFfff0e6), borderRadius: BorderRadius.circular(6.rpx)),
                      child: Text(
                        tag[0],
                        style: TextStyle(fontSize: 20.rpx, color: const Color(0xffee7219)),
                      ))
              ],),
            ),
          Padding(
            padding: EdgeInsets.only(top: 40.px),
            child: Row(children: [
              subWidget(0),
              Expanded(child: subWidget(1)),
              subWidget(2),
            ],),
          ),
          SizedBox(height: 38.px,),
          // Expanded(child: Container(height: 1.px,color: MyColors.colorF1F1F1,)),
          Text("路书亮点",style: TextStyle(fontSize: 32.px,color: MyColors.color1A1A1A,fontWeight: FontWeight.w500),),
          SizedBox(height: 24.px,),
          Text(msg,style: TextStyle(fontSize: 28.px,color: MyColors.color666666),),
        ],),
      ),
    );
  }

  Widget subWidget(int index){
    String img ;
    String title ;
    if(index == 0){
      img =  AhaUtils.getImagePath('ls_icon_return');
      title = "${widget.lineDetailModel.totalDistance}KM";
    }else  if(index == 1){
      img =  AhaUtils.getImagePath('ls_icon_calendar');
      title = getPlayTime();
    }else{
      img =  AhaUtils.getImagePath('ls_icon_star');
      title = '${widget.lineDetailModel.score}分';
    }
    return Row(mainAxisAlignment: MainAxisAlignment.center, children: [
      Image(image: AssetImage(img),width: 40.px,height: 40.px,),
      SizedBox(width: 24.px,),
      Text(title,style: TextStyle(fontSize: 28.px,color: MyColors.color666666),),
    ],);
  }
  String getPlayTime (){
    String time = '';
    List<PlaySeason?>? pay = widget.lineDetailModel.playSeason;
    int? len = pay?.length ;

    for(int i = 0;i<len!;i++){
      PlaySeason? season = pay![i];
      if(i == 0){
        time = '${season!.start}-${season.end}月';
      }else if(i == 1){
          time = '$time, ${season!.start}-${season.end}月';
      }
    }
    return time;
  }
}
